/*
* Copyright 2011 Stepan Riha
*
* This file may be used under the terms of of the
* GNU General Public License Version 3 or later (the "GPL"),
* http://www.gnu.org/licenses/gpl.html
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*/
var Database = (function() {

	var getDatabase = function() {
		var db = openDatabase('bookburrito', '', 'Book Burrito Preferences', 5*1024*1024);
		
		if(db.version != '1.0') {
			db.changeVersion('', '1.0', function (t) {
				t.executeSql('CREATE TABLE libraries (name, title, link, dont, have, missing, match_isbn, is_community, version)', [], null, error);
				t.executeSql('CREATE TABLE custom (name, title, link, dont, have, missing, match_isbn, is_community, version)', [], null, error);
			});
		}
		
		return db;
	}
	
	getDatabase();
	
	var error = function(tx, error) {
		alert("BookBurrit SQL Error " + error);
	};


	var storeAllLibraries = function(libs, custom) {
		getDatabase().transaction(function(tx) {
			tx.executeSql('DELETE FROM libraries', [], null, error);
			for(var i = 0; i < libs.length; i++) {
				var lib = libs[i];
				tx.executeSql('INSERT INTO libraries (name, title, link, dont, have, missing, match_isbn, is_community) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
					[lib.name, lib.title, lib.link, lib.dont, lib.have, lib.missing || null, lib.match_isbn || null, lib.is_community || null], null, error);
			}

			libs = custom;
			tx.executeSql('DELETE FROM custom', [], null, error);
			for(var i = 0; i < libs.length; i++) {
				var lib = libs[i];
				tx.executeSql('INSERT INTO custom (name, title, link, dont, have, missing, match_isbn) VALUES (?, ?, ?, ?, ?, ?, ?)',
					[lib.name, lib.title, lib.link, lib.dont, lib.have, lib.missing || null, lib.match_isbn || null], null, error);
			}
		}, error);
	};

	var getLibraries = function(cb) {
		getDatabase().readTransaction(function(tx) {
			tx.executeSql('SELECT * FROM libraries ORDER BY title', [], function(tx, results) {
				var len = results.rows.length, i;
				var libs = [];
				for(i = 0; i < len; i++) {
					var row = results.rows.item(i);
					libs.push(new Library(row));
				}
				cb(libs);
			});
		});
	};
	
	var getCustomLibraries = function(cb) {
		getDatabase().readTransaction(function(tx) {
			tx.executeSql('SELECT * FROM custom ORDER BY title', [], function(tx, results) {
				var len = results.rows.length, i;
				var libs = [];
				for(i = 0; i < len; i++) {
					var row = results.rows.item(i);
					libs.push(new CustomLibrary(row));
				}
				cb(libs);
			});
		});
	};
	
	var getLibrariesByName = function(cb) {
		getDatabase().readTransaction(function(tx) {
			tx.executeSql('SELECT * FROM libraries', [], function(tx, results) {
				var len = results.rows.length, i;
				var libs = {};
				for(i = 0; i < len; i++) {
					var row = results.rows.item(i);
					libs[row.name] = new Library(row);
				}
				cb(libs);
			});
		});
	};
	
	var getLibraryByName = function(name, cb) {
		getDatabase().readTransaction(function(tx) {
			tx.executeSql('SELECT * FROM libraries WHERE name = ?', [name], function(tx, results) {
				var len = results.rows.length, i;
				var libs = {};
				for(i = 0; i < len; i++) {
					var row = results.rows.item(i);
					cb(new Library(row));
					break;
				}
			});
		});
	};
	
	var getCustomLibraryByName = function(name, cb) {
		getDatabase().readTransaction(function(tx) {
			tx.executeSql('SELECT * FROM custom WHERE name = ?', [name], function(tx, results) {
				var len = results.rows.length, i;
				var libs = {};
				for(i = 0; i < len; i++) {
					var row = results.rows.item(i);
					cb(new CustomLibrary(row));
					break;
				}
			});
		});
	};
	
	return {
		getLibraries: getLibraries,
		getLibrariesByName: getLibrariesByName,
		getLibraryByName: getLibraryByName,
		
		getCustomLibraries: getCustomLibraries,
		getCustomLibraryByName: getCustomLibraryByName,

		storeAllLibraries: storeAllLibraries
	};

})();

